using System;

namespace DotConferenceSql
{
    /// <summary>
    /// Generic ORDER BY element
    /// </summary>
    public class OrderByQueryElement : QueryElementBase
    {
        /// <summary>
        /// Order to specify to the ORDER BY caluse
        /// </summary>
        public enum Order
        {
            /// <summary>
            /// ASC
            /// </summary>
            Ascendant,
            /// <summary>
            /// DESC
            /// </summary>
            Descendant
        }

        private Order m_order;
        private string m_field;

        /// <summary>
        /// Gets or sets the field.
        /// </summary>
        /// <value>The field.</value>
        public string Field
        {
            get
            {
                return m_field;
            }
            set
            {
                m_field = value;
            }
        }

        /// <summary>
        /// Gets or sets the ordering.
        /// </summary>
        /// <value>The ordering.</value>
        public Order Ordering
        {
            get
            {
                return m_order;
            }
            set
            {
                m_order = value;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="OrderByQueryElement"/> class.
        /// </summary>
        /// <param name="field">The field.</param>
        /// <param name="order">The order.</param>
        public OrderByQueryElement(string field, Order order)
            : base()
        {
            Field = field;
            Ordering = order;
            Type = QueryElementType.OrderByClause;
        }

        /// <summary>
        /// Generate the part of the query for this element.
        /// </summary>
        /// <returns>generated SQL fragment</returns>
        public override string BuildTextQuery()
        {
            string orderClause = String.Empty;
            if (Ordering == Order.Ascendant)
            {
                orderClause = "ASC";
            }
            else if (Ordering == Order.Descendant)
            {
                orderClause = "DESC";
            }

            return String.Format("{0} {1}", Field, orderClause);
        }
    }
}